package com.pwt.repository;

import com.pwt.repository.base.BaseRepository;
import com.pwt.entity.Role;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.Set;

/**
 * Role Repository层
 *
 * @author auto generated
 * @date 2021-08-27 15:18:32
 */
public interface RoleRepository extends BaseRepository<Role, String> {

    @Query(nativeQuery = true,value = "select r.* from [role] r left join [user_role] ur on r.id = ur.role_id left join [user] u on ur.user_id = u.id where u.username = ?1")
    Set<Role> findByUsername(String username);

    @Query(nativeQuery = true,value = "select r.id from [role] r left join [user_role] ur on r.id = ur.role_id left join [user] u on ur.user_id = u.id where u.username = ?1")
    Set<String> getByUsername(String username);

    @Query(nativeQuery = true,value = "select r.id from [role] r left join [user_role] ur on r.id = ur.role_id left join [user] u on ur.user_id = u.id where u.id = ?1")
    Set<String> getByUserId(String username);

    // hql
    @Query("select count(1) from Role where sign = ?1")
    int countBySign(String sign);

    @Modifying
    @Query(nativeQuery = true,value = "delete [user_role] where user_id = ?1")
    void deleteByUserId(String userId);

    @Modifying
    @Query(nativeQuery = true,value = "insert into [user_role] (user_id,role_id,id) values (?1,?2,?3)")
    void saveUserRole(String userId, String roleId,String id);

    @Query(nativeQuery = true,value = "select top 1  * from role where sign = ?1")
    Role findBySign(String sign);
}
